<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFromRef" :model="queryParams" inline label-width="68px">
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="queryParams.createTimeArr"
            :editable="false"
            class="!w-[240px]"
            end-placeholder="截止时间"
            range-separator="~"
            start-placeholder="开始时间"
            type="daterange"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            v-hasPerm="['sys:captchaLog:query']"
            class="filter-item"
            type="primary"
            @click="handleQuery"
          >
            <i-ep-search />
            搜索
          </el-button>

          <el-button @click="handleResetQuery">
            <i-ep-refresh />
            重置
          </el-button>

          <el-button
            v-hasPerm="['sys:captchaLog:deleteAll']"
            type="danger"
            @click="handleDeleteAll()"
          >
            <template #icon>
              <i-ep-delete />
            </template>
            全部删除
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card class="table-container" shadow="never">
      <el-table v-loading="loading" :data="list" stripe style="width: 100%">
        <el-table-column label="UUID" prop="uuid" width="300" />
        <el-table-column label="验证码" prop="captcha" width="150" />
        <el-table-column label="过期时间" prop="expireTime" width="200" />

        <el-table-column label="IP" prop="ip" width="150" />
        <el-table-column label="浏览器" prop="browser" width="150" />
        <el-table-column label="系统" prop="os" width="150" />
        <el-table-column label="用户UA" min-width="100" prop="userAgent" width="400" />

        <el-table-column fixed="right" label="操作" width="80">
          <template #default="scope">
            <el-button
              v-hasPerm="['sys:captchaLog:delete']"
              link
              size="small"
              type="danger"
              @click.stop="handleDelete(scope.row.id)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        v-model:total="total"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script setup>
defineOptions({
  name: 'CaptchaLog',
  inheritAttrs: false
})

import { ElForm, ElTable } from 'element-plus'
import { ref, reactive, onMounted } from 'vue'
import request from '@/utils/request'

const list = ref([])
const total = ref(0)
const loading = ref(false)

const queryFromRef = ref()

const BASE_URL = '/api/yyplatform/admin/sys/captchaLog/'
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  createTimeArr: []
})

function handleDeleteAll() {
  ElMessageBox.confirm('确认要执行全部删除吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    request({
      url: `${BASE_URL}/deleteAll`,
      method: 'post'
    }).then(() => {
      handleQuery()
    })
  })
}

function handleDelete(id) {
  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    request({
      url: `${BASE_URL}/delete`,
      method: 'post',
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      data: {
        id: id
      }
    }).then(() => {
      handleQuery()
    })
  })
}

async function handleResetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 20
  }
  await handleQuery()
}

function handleQuery() {
  console.log(queryParams, 'queryParams')

  loading.value = true
  request({
    url: `${BASE_URL}/query`,
    method: 'get',
    params: {
      ...queryParams.value
    }
  }).then((res) => {
    console.log(res, 'res')

    loading.value = false
    list.value = res.content
    total.value = parseInt(res.totalElements)
  })
}

onMounted(() => {
  handleQuery()
})
</script>
